import config from '../../utils/config'
import {request} from "../../utils/request";
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        custom:wx.getMenuButtonBoundingClientRect(),
        value1: [],
        options2: [],
        value2: [],
        visible2:false,
        title2:'',
        province:'',
        city:'',
        county:'',
        keyword:'',
        phone:'',
        address:'',
        main:0,
        id:'',
        list:[],
        date:'',
        dialogHide:true,
        currentStatus:'',
        currentId:'',
        currentIndex:null,
        currentGood:null
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getRegion()
        this.setData({id:options.id})
        this.getList()
    },
    onOpen1() {
        this.setData({ visible2: true })
    },
    onClose1() {
        this.setData({ visible2: false })
    },
    onChange1(e) {
        if(e.detail.options[0]){
            this.setData({province:e.detail.options[0].label,})
        }
        if(e.detail.options[1]){
            this.setData({city:e.detail.options[1].label,})
        }
        if(e.detail.options[2]){
            this.setData({county:e.detail.options[2].label})
        }
        console.log(this.data.province,this.data.city,this.data.county)
    },
    getRegion(){
        request({url:"/miniprogram/system/asyncRegion"}).then(res=>{this.setData({options2:res.data})})
    },
    changeValue(e) {
        var field = e.currentTarget.dataset.field,
            value = e.detail.value
        this.setData({[field]: value})
    },
    search(){
        var {keyword,province,city,county} = this.data
        request({url:"/admin/good/getAllSole",data:{id:this.data.id,keyword,province,city,county}}).then(res=>{
            this.setData({
                list:res.data
            })
        })
    },
    getList(){
        request({url:"/admin/good/getAllSole",data:{id:this.data.id}}).then(res=>{
            this.setData({
                list:res.data
            })
        })
    },
    changeSoleStatus(e){
        var {status,id,index,good} = e.currentTarget.dataset,
            list = this.data.list
        if(status == 2){
            request({
                url:'/admin/good/allowSole',
                data:{id,status,good_id:good},
                type:'post'
            }).then(res=>{
                config.toast(res.msg,201,()=>{
                    list[index].status = status
                    this.setData({
                        list:list
                    })
                })
            })
        }else{
            this.setData({
                dialogHide:false,
                currentStatus:status,
                currentId:id,
                currentIndex:index,
                currentGood:good
            })
        }
    },
    bindDateChange(e){
        this.setData({date:e.detail.value})
    },
    subAgree(){
        var status = this.data.currentStatus,
            id = this.data.currentId,
            index = this.data.currentIndex,
            good = this.data.currentGood,
            list = this.data.list,
            date = this.data.date
        request({
            url:'/admin/good/allowSole',
            data:{id,status,good_id:good,date},
            type:'post'
        }).then(res=>{
            config.toast(res.msg,201,()=>{
                list[index].status = status
                this.setData({
                    list:list,
                    date:''
                })
                if(res.code == 200){
                    this.setData({dialogHide:true})
                }
            })
        })
    }
})